// @File:           logger_c.cpp 
// @ProjectName:    gee_logger_c
// @Description:    ${description}
// @Author:         wujehy
// @Email:          wujehy@qq.com
// @Time:           2021-03-28 23:40

#include "logger_c.h"
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
#include <stdio.h>
#include <arpa/inet.h>
#include "geelogger.pb-c.h"
#include <sys/time.h>

static int socket_fd = -1;
static struct sockaddr_in logger_server_udp;

void init_logger_service(const char *host, int port) {
    // 创建一个UDP套接字
    if ((socket_fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
        fprintf(stderr, "init_logger_service socket error");
        return;
    }
    memset(&logger_server_udp, 0, sizeof(logger_server_udp));
    logger_server_udp.sin_family = AF_INET;
    logger_server_udp.sin_port = htons(port);
    logger_server_udp.sin_addr.s_addr = inet_addr(host);

}

void deinit_logger_service() {
    if (socket_fd == -1) {
        // 为初始化 不需要处理
        return;
    }
    //否则需要处理
    int ret = close(socket_fd);
    if (ret == 0) {
        socket_fd = -1 ;
        fprintf(stderr, "close fd success\n");
    } else {
        fprintf(stderr, "close fd fail\n");
    }
}

void upload_logger_info_to_logger_server(
        unsigned long long secure_key,
        int logger_level,
        unsigned long long client_time,
        unsigned long long app_id,
        unsigned long long mod_type,
        unsigned long long sub_type,
        const char *package_name,
        const char *class_name,
        const char *func_name,
        int line_number,
        const char *msg) {

    if (socket_fd == -1) {
        // 为初始化日志
        return;
    }
    int message_len = strlen(msg);
    int i = 0;
    char buffer_msg[1025] = {0};
    char send_package_buffer[1400] = {0};
    GeeJoan__Protocol__Logger__GeeLogger logger_info_msg = GEE_JOAN__PROTOCOL__LOGGER__GEE_LOGGER__INIT;
    logger_info_msg.gee = "Gee";
    logger_info_msg.app_id = app_id;
    logger_info_msg.line_number = line_number;
    logger_info_msg.logger_level = logger_level;
    logger_info_msg.secure_key = secure_key;
    logger_info_msg.mod_type = mod_type;
    logger_info_msg.sub_type = sub_type;
    logger_info_msg.client_time = client_time;
    logger_info_msg.package_name = (char *) package_name;
    logger_info_msg.class_name = (char *) class_name;
    logger_info_msg.func_name = (char *) func_name;
    for (i = 0; i < message_len; i += 1024) {
        // 一个包只能 1024
        if (message_len / 1024 > 1) {
            memcpy(buffer_msg, &msg[i], 1024);
            buffer_msg[1024] = 0 ;
        } else {
            int temp_end = (message_len % 1024);
            memcpy(buffer_msg, &msg[i],temp_end );
            buffer_msg[temp_end] = 0 ;
        }
        logger_info_msg.msg = buffer_msg;
//        printf("current[%d] : %s \n" , i, buffer_msg);
        int send_len = gee_joan__protocol__logger__gee_logger__pack(&logger_info_msg, send_package_buffer);
        // 发包
        int real_send_len = sendto(socket_fd, send_package_buffer, send_len, 0, (struct sockaddr *) &logger_server_udp, sizeof(logger_server_udp));
        if (real_send_len == send_len) {
            //发送成功
        }
    }
    // 释放内存
}


void upload_logger_info_to_logger_server_tesgt_debug(
        unsigned long long mod_type,
        unsigned long long sub_type,
        const char *class_name,
        const char *func_name,
        int line_number,
        const char *msg) {
    struct timeval tv;
    gettimeofday(&tv, NULL);

    unsigned long long client_time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
    upload_logger_info_to_logger_server(
            SECURE_KEY,
            1,
            client_time,
            CLIENT_APP_ID, mod_type, sub_type, CLIENT_PACKAGE_NAME,
            class_name, func_name, line_number, msg);
}